package controller.parameterpanels;

import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;

import model.auxillary.DuplicateToolNameException;
import model.auxillary.ParameterCollection;
import model.auxillary.ParameterList;
import view.AlphaTextField;

/**
 * The {@code ListenerToolTextField} class controls the user written
 * content of an {@code AlphaTextField} that changes the name of a tool.
 * @author Oliver Eckstein
 *
 */
public class ListenerToolTextField implements DocumentListener, FocusListener {
	
	/**
	 * The text field to be controlled.
	 */
	private AlphaTextField textField;
	
	/**
	 * The tool to be controlled.
	 */
	private ParameterCollection tool;
	
	/**
	 * The {@code ParameterList} that holds the tool.
	 */
	private ParameterList paramList;
	
	/**
	 * The controller of the parameter panel that shows the
	 * {@code AlphaTextField}.
	 */
	private CtrInputParameterPanel controller;
	
	/**
	 * To create a new {@code ListenerToolTextField}.
	 * @param textField the text field that gets controlled.
	 * @param tool the tool that gets controlled by this listener.
	 * @param paramList the {@code ParameterList} that holds the tool.
	 * @param controller the controller of the view where the text field is
	 * located.
	 */
	public ListenerToolTextField(
			AlphaTextField textField,
			ParameterCollection tool,
			ParameterList paramList,
			CtrInputParameterPanel controller) {
		this.textField = textField;
		this.tool = tool;
		this.paramList = paramList;
		this.controller = controller;
		textField.addFocusListener(this);
	}

	@Override
	public void changedUpdate(DocumentEvent e) {
		// TODO Auto-generated method stub
	}

	@Override
	public void insertUpdate(DocumentEvent e) {
		updateValue(e);
	}

	@Override
	public void removeUpdate(DocumentEvent e) {
		updateValue(e);
	}
	
	/**
	 * To change the name of a tool.
	 * @param e the {@code DocumentEvent}.
	 */
	private void updateValue(DocumentEvent e) {
		String txt = "";
		try {
            txt = e.getDocument().getText(0, e.getDocument().getLength());
        } catch (BadLocationException e1) {            
            e1.printStackTrace();
        }
				
		if(txt.equals("")) {
			return;
		}
		
		try {
			// Throw error when user tries to duplicate a tool's name.
			paramList.setToolName(tool, txt);
			if(textField.getErrorStatus()) {
				textField.clearError();
				controller.removeInvalidInput();
			}
		} catch(DuplicateToolNameException dtn) {
			textField.showError();
			controller.addInvalidInput();
		}
	}

	@Override
	public void focusGained(FocusEvent arg0) {		
		textField.selectAll();		
	}

	@Override
	public void focusLost(FocusEvent arg0) {
		// Change the border's text.
		textField.setCaretPosition(0);
		String newToolName = textField.getText();		
		CtrToolPanel toolPanelController = (CtrToolPanel) controller;
		toolPanelController.changeBorderText(textField, newToolName);
	}
}
